package cn.iocoder.yudao.module.blog.service.postertype;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.postertype.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.postertype.PosterTypeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.postertype.PosterTypeConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.postertype.PosterTypeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 海报类型 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class PosterTypeServiceImpl implements PosterTypeService {

    @Resource
    private PosterTypeMapper posterTypeMapper;

    @Override
    public Long createPosterType(PosterTypeCreateReqVO createReqVO) {
        // 插入
        PosterTypeDO posterType = PosterTypeConvert.INSTANCE.convert(createReqVO);
        posterTypeMapper.insert(posterType);
        // 返回
        return posterType.getId();
    }

    @Override
    public void updatePosterType(PosterTypeUpdateReqVO updateReqVO) {
        // 校验存在
        validatePosterTypeExists(updateReqVO.getId());
        // 更新
        PosterTypeDO updateObj = PosterTypeConvert.INSTANCE.convert(updateReqVO);
        posterTypeMapper.updateById(updateObj);
    }

    @Override
    public void deletePosterType(Long id) {
        // 校验存在
        validatePosterTypeExists(id);
        // 删除
        posterTypeMapper.deleteById(id);
    }

    private void validatePosterTypeExists(Long id) {
        if (posterTypeMapper.selectById(id) == null) {
            throw exception(POSTER_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public PosterTypeDO getPosterType(Long id) {
        return posterTypeMapper.selectById(id);
    }

    @Override
    public List<PosterTypeDO> getPosterTypeList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return posterTypeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PosterTypeDO> getPosterTypePage(PosterTypePageReqVO pageReqVO) {
        return posterTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PosterTypeDO> getAll(){
        return posterTypeMapper.selectList();
    }

    @Override
    public List<PosterTypeDO> getPosterTypeList(PosterTypeExportReqVO exportReqVO) {
        return posterTypeMapper.selectList(exportReqVO);
    }

}
